<template>
	<view class="content">
		<view class="padding ">
			<view class="header">
				<view class="text">实付金额</view>
				<view class="money">{{Number(data.pay_price).toFixed(2)}}</view>
			</view>
			<view class="methods ffffff border">
				<view class="title padding">选择支付方式</view>
				<view class="methods-item padding ffffff flexbetween" @click="changeIndex(1)">
					<view class="left flexcenter">
						<image src="/static/image/home/wiexin.png" mode="" class="logo"></image>
						<view class="text_a color_san margin_left2">微信</view>
					</view>
					<view class="right">
						<image v-show="activeIndex==1" src="/static/image/home/xuanzhong.png"
							style="width:34rpx;height: 34rpx;">
						</image>
						<image v-show="activeIndex!=1" src="/static/image/home/wiexuanzhong.png"
							style="width: 34rpx;height: 34rpx;"></image>
					</view>
				</view>
				<view class="cent padding ffffff flexleft   margin-top" @click="changeIndex(0)">
					<image src="/static/image/home/yue.png" mode="" class="logo"></image>
					<view class="cent-content">
						<text class="text_a color_san  margin_left2">余额</text>
					</view>
					<view class="">
						<image  v-show="activeIndex==0" src="/static/image/home/xuanzhong.png" style="width: 34rpx;height: 34rpx;"></image>
						<image  v-show="activeIndex!=0" src="/static/image/home/wiexuanzhong.png"
							style="width: 34rpx;height: 34rpx;"></image>
					</view>
				</view>
			</view>
			<view class="buyBtn" @click="gopay">立即支付</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				switchValue: false,
				activeIndex: 1, //1微信支付，0余额支付
				pay_price: "",
				userinfo: {},
				wxpay: {},
				type: 1,
				allscore: 0, //0折扣 1不折
				data: {},
				code:"",
				openid:"",
			};
		},
		onLoad(options) {
			console.log("接收参数", options)
			let _this = this
			let data = JSON.parse(decodeURIComponent(options.msg));
			_this.data = data
			this.wxlogin()
			// _this.getinfo()
		},
		methods: {
			wxlogin() {
				let _this = this
				uni.login({
					success(res) {
						console.log(res,"yemian ")
						if (res.code) {
							_this.code = res.code
							uni.setStorageSync('code', res.code);
							httpRequest.request('api/user/wxlogin', 'POST', {
								code:res.code
							}).then(resp => {
								_this.openid = resp.data.third.third_info.openid
							}).catch(err=>{
								console.log(err,"获取微信信息")
							})
						}
					}
				})
			},
			getinfo() {
				httpRequest.request('api/user/index', 'GET', {}).then(res => {
					this.userinfo = res.data
				})
			},
			changeIndex(item) {
				this.activeIndex = item
				console.log(this.activeIndex,"999");
			},
			gopay() {
				let _this = this
				httpRequest.modal('提示', '确认支付当前订单吗?', true, (res) => {
					if(res){
						uni.showLoading({
							title: '请稍候...',
							mask:true,
						})
						httpRequest.request('/api/order/payment', 'POST', {
							order_pay_no: this.data.id,
							pay_type: this.activeIndex,
							openid:_this.openid
						}, false, false, true).then(res => {
							if(res.code==1){
								if (this.activeIndex == 1) { //微信支付流程
									this.wxpay = res.data.wxconfig
									this.callPayMent()
								} else { //余额支付流程
									setTimeout(()=>{
										uni.navigateBack(1)
									},500)
								}
							}
							uni.hideLoading()
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 4000
							})
						}).catch(err => {
							httpRequest.toast(err.msg);
						})
					}
				})
			},
			callPayMent() {
				let _this = this;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: _this.wxpay.timeStamp,
					nonceStr: _this.wxpay.nonceStr,
					package: _this.wxpay.package,
					signType: _this.wxpay.signType,
					paySign: _this.wxpay.paySign,
					success: function(res) {
						setTimeout(()=>{
							uni.navigateBack(1)
						},500)
					},
					fail: function(err) {
						
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.header {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 17rpx 0 38rpx 0;
		margin-bottom: 38rpx;
	}

	.text {
		font-size: 30rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #000000;
	}

	.money {
		font-size: 71rpx;
		font-family: DIN-Bold, DIN;
		font-weight: bold;
		color: #000000;
		margin-top: 33rpx;
	}

	.title {
		font-size: 30rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #000000;
	}

	.cent {
		justify-content: space-between;
	}

	.cent-content {
		flex: 1;
	}

	.logo {
		width: 32rpx;
		height: 32rpx;
	}

	.kedikou {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.buyBtn {
		position: fixed;
		bottom: 60rpx;
		background-color: pink;
		width: calc(100vw - 60rpx);
		text-align: center;
		padding: 18rpx 0;
		background-color: #d48b39;
		color: #fff;
		border-radius: 64rpx;
	}

	.methods-item {
		border-bottom: 2rpx solid #fafafa;
	}

	.methods-item:last-child {
		border-bottom: 0;
	}
</style>